package sdc.media.motorbike.Model;

import sdc.media.motorbike.Controller.Constant;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Pool.Poolable;

public class Object implements Poolable {
	
	public enum STATE {
		EXIST, 
		DIE
	}
	protected STATE state;
	
	private Vector2 position;
	public Vector2 originalPosition;
	public float distance;
	private TextureRegion image;
	private Sprite sprite;
	
	private Body body;
	private BodyDef bodyDef;
	private FixtureDef fixtureDef;
	private String name;
	
	public Object() {
		// TODO Auto-generated constructor stub
		state = STATE.DIE;
		this.position = new Vector2();
		this.distance = 0;
		this.originalPosition = new Vector2();
		this.bodyDef = new BodyDef();
		this.fixtureDef = new FixtureDef();
	}
	
	public void init(World world, String name, Vector2 position) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.position.set(position);
		this.originalPosition.set(position);
		this.bodyDef.type = BodyType.DynamicBody;
		this.bodyDef.position.set(position);
		this.fixtureDef.density = 1;
		this.fixtureDef.restitution = 0.1f;
		state = STATE.EXIST;
	}
	
	public STATE getState() {
		return state;
	}
	
	public void setState(STATE state) {
		this.state = state;
	}
	
	public Body getBody() {
		return body;
	}
	
	public BodyDef getBodyDef() {
		return bodyDef;
	}
	
	public FixtureDef getFixtureDef() {
		return fixtureDef;
	}
	
	public String getName() {
		return name;
	}
	
	public void setBody(Body body) {
		this.body = body;
	}
	
	public void setBodyDef(BodyDef bodyDef) {
		this.bodyDef = bodyDef;
	}
	
	public void setFixtureDef(FixtureDef fixtureDef) {
		this.fixtureDef = fixtureDef;
	}
	
	public TextureRegion getImage() {
		return image;
	}
	
	public void setImage(TextureRegion image) {
		this.image = image;
	}
	
	public Sprite getSprite() {
		return sprite;
	}
	
	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
	}
	
	public Vector2 getPosition() {
		return position;
	}
	
	public void setPosition(Vector2 position) {
		this.position = position;
	}
	
	public float getWidth() {
		// TODO Auto-generated method stub
		return getImage().getRegionWidth() * Gdx.graphics.getWidth() / 480.0f;
	}

	public float getHeight() {
		// TODO Auto-generated method stub
		return getImage().getRegionHeight() * Gdx.graphics.getHeight() / 800.0f;
	}
	
	public void loadSpriteToBody() {
		// TODO Auto-generated method stub
		getSprite().setSize(getWidth() / Constant.PIXEL_PER_METER, getHeight() / Constant.PIXEL_PER_METER);
		getSprite().setOrigin(getSprite().getWidth() / 2, getSprite().getHeight() / 2);
		getBody().setUserData(new UserData(getName(), getSprite()));
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		state = STATE.DIE;
		this.position = new Vector2();
		this.bodyDef = new BodyDef();
		this.fixtureDef = new FixtureDef();
	}
	
}
